package cn.lbz.page;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

public class peopleonline implements HttpSessionListener{
    /**
     * 监听session创建
     */
    public void sessionCreated(HttpSessionEvent se) {
        ServletContext context = se.getSession().getServletContext();
        Integer num = (Integer) context.getAttribute("online_num");
        if(num == null)
            num = 0;
        ++num;
        context.setAttribute("online_num",num);
        System.out.println("新增一位用户");
    }

    /**
     * 监听session销毁
     */
    public void sessionDestroyed(HttpSessionEvent se) {
        ServletContext context = se.getSession().getServletContext();
        Integer num = (Integer) context.getAttribute("online_num");
        if(num == null)
            num = 0;
        else
            --num;
        context.setAttribute("online_num",num);
        System.out.println("减少一位用户");
    }
}


